<template>
<!-- 坏账确认 -->
  <section class="component bad-debts-confirmation">
    <data-form :model="searchModel" :page="pageService" @onSearch="searchDataBox">
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="searchModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="verifyBatchNumber">
          <el-input v-model="searchModel.verifyBatchNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户编号" prop="personalNo">
          <el-input v-model="searchModel.personalNo"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="searchModel.personalName"></el-input>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataSet" :page="pageService"  @onPageChange="refreshDataBox" :selectionList.sync="dataBoxSelectionList" @update:selectionList="onSelectionListChange">
      <template slot="columns">
        <el-table-column prop="verifyBatchNumber" label="申请批次号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="caseNumber" label="借据号" :min-width="$helper.getColumnWidth(6)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="personalNo" label="客户编号" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="area" label="地区" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $city.getInheritNames(Number(row.area))" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overdueAmount" label="案件金额(元)" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overduePeriod" label="逾期阶段" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="sysCode" label="系统来源" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.encryptSysCode(row.sysCode)">
        </el-table-column>
        <el-table-column prop="loanChannel" label="放款渠道" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.encryptLoanChannel(row.loanChannel)">
        </el-table-column>
        <el-table-column prop="productName" label="产品名称" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="perDueDate" label="还款日" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="commonFlag" label="附件是否提交" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.dictConvert(row.commonFlag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="verifyStatus" label="核销状态" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.dictConvert(row.verifyStatus,'VerifyStatus')">
        </el-table-column>
        <el-table-column prop="followInTime" label="案件流入时间" :formatter="(row) => $filter.dateTimeFormat(row.followInTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
      </template>
    </data-box>
    <el-form>
      <el-form-item label="案件金额合计">
        {{`${$filter.toThousands(this.totalOverdueAmount)}元`}}
      </el-form-item>
      <el-form-item label="对应坏账金额合计">
        {{`${$filter.toThousands(this.totalBadAmount)}元`}}
      </el-form-item>
      <el-form-item label="上传文件">
        <file-upload ref="file-upload" :fileList.sync="fileList"></file-upload>
      </el-form-item>
      <el-form-item label="申请原因">
        <el-input type="textarea" :maxlength="500" v-model="applyDescription"></el-input>
      </el-form-item>
    </el-form>
    <div class="operate-buttons">
      <el-button @click="close">取消</el-button>
      <el-button @click="submit">确定</el-button>
    </div>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import FileUpload from "~/components/common/file-upload.vue";
import { PageService } from "~/utils/page.service";
import { CaseApplyRecordService } from "~/services/domain-service/case-apply-record.service";
import { Emit,Prop,Watch } from 'vue-property-decorator';
import { CaseVerifyService } from "~/services/domain-service/case-verify.service";

// @Auth(414)
@Layout('workspace')
@Component({
  components: {
    DataForm,
    DataBox,
    FileUpload
  }
})
export default class BadDebtsConfirmation extends Vue {
  @Dependencies(PageService) private pageService: PageService;
  @Dependencies(CaseApplyRecordService) private caseApplyRecordService: CaseApplyRecordService;
  @Dependencies(CaseVerifyService)
  private caseVerifyService: CaseVerifyService;

  private importLoading: boolean = false
  private fileList: any = []
  private applyDescription: any = ''
  private dataBoxSelectionList: any = []
  private dataBoxSelectionLists: any = []
  private dataSet: any[] = []
  private totalBadAmount: any = ''
  private totalOverdueAmount: any = ''
  private totalBadAmountCaseIds: any[] = []
  private searchModel: any = {
    caseNumber: "",
    personalName: "",
    personalNo:'',
    verifyBatchNumber: "",
  }
  private submitModel: any = {
    caseIds: [],
    fileIds: [],
    applyDescription:'',
    confirm: '0'
  }
  private rules: any = {
    applyDescription: [{ required: true, message: '请输入申请原因',trigger: "blur" }],
  };
  @Prop({ required: true })
  private selectionList;
  @Prop({ required: true })
  private BaseVerifyBatchNumber;
  @Prop({ required: true })
  private BaseVerifyStatus;
  @Prop()
  private applyRecordfileList;
  @Prop()
  private applyRecordDescription;
  // @Prop()
  // private totalBadAmount;
  @Prop()
  private BadAmountBase;
  @Emit('refreshData')
  private refreshData() { }
  @Emit('close')
  private close() {
    this.refresh()
  }
  @Watch('applyRecordfileList', { immediate: true })
  private applyRecordfileListWatch() {
    this.fileList = this.applyRecordfileList?this.applyRecordfileList:[]
    this.applyDescription = this.applyRecordDescription?this.applyRecordDescription:''
  }
  @Watch('BadAmountBase', { immediate: true })
  private BadAmountBaseWatch() {
    this.refreshDataBox()
    this.dataBoxSelectionList = this.selectionList
    this.dataBoxSelectionLists = this.selectionList
    this.totalBadAmountCaseIds = []
    this.dataBoxSelectionLists.map(x=>{
      this.totalBadAmountCaseIds.push(x.id)
    })
    this.getBadAmount(this.totalBadAmountCaseIds)
    this.getTotalOverdueAmount(this.totalBadAmountCaseIds)
  }

  private refreshDataBox() {
    const ids =this.selectionList? this.selectionList.map((v) => v.id):'';
    const request: any = {
      ...this.searchModel,
      caseIds: ids,
    }
    this.caseVerifyService.getWaitVerifyConfirmInfos(request,this.pageService).subscribe(
      data => {
        this.dataSet = data
        this.dataBoxSelectionList = data
        if(this.dataSet){
          this.dataSet.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
      }
    )
  }
  private searchDataBox() {
    const ids =this.dataBoxSelectionLists? this.dataBoxSelectionLists.map((v) => v.id):'';
    const request: any = {
      ...this.searchModel,
      caseIds: ids,
    }
    this.caseVerifyService.getWaitVerifyConfirmInfos(request,this.pageService).subscribe(
      data => {
        this.dataSet = data
        this.dataBoxSelectionList = data
        if(this.dataSet){
          this.dataSet.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
      }
    )
  }
  private onSelectionListChange(val,row) {
    if(row){
      for(let i = 0; i < this.dataBoxSelectionLists.length; i++) {
        if(this.dataBoxSelectionLists[i].caseNumber === row.caseNumber) {
          this.dataBoxSelectionLists.splice(i, 1);
          this.dataBoxSelectionList.splice(i, 1);
          break;
        }
      }
    }
    this.totalBadAmountCaseIds = []
    if(this.dataBoxSelectionLists.length !== 0){
      this.dataBoxSelectionLists.map(x=>{
        this.totalBadAmountCaseIds.push(x.id)
      })
      this.getBadAmount(this.totalBadAmountCaseIds)
      this.getTotalOverdueAmount(this.totalBadAmountCaseIds)
    }else{
      this.totalBadAmount = '0.00'
      this.totalOverdueAmount = '0.00'
    }
    this.searchDataBox()
  }
  /**
   * 确认核对
   */
  private submit() {
    this.submitModel.caseIds = []
    this.submitModel.fileIds = []
    if (this.fileList.length === 0) {
      this.$message('请先上传文件')
      return
    }
    if (this.applyDescription === '') {
      this.$message('请输入申请原因')
      return
    }
    if(this.BaseVerifyStatus === 'DISMISSED'){
      this.fileList.map(x=>{
        if(x.response){
          this.submitModel.fileIds.push(x.response.id)
        }
        if(x.id){
          this.submitModel.fileIds.push(x.id)
        }
      })
    }else{
      this.fileList.map(x=>{
        this.submitModel.fileIds.push(x.response.id)
      })
    }
    this.dataBoxSelectionLists.map(x=>{
      this.submitModel.caseIds.push(x.id)
      this.submitModel.verifyBatchNumber = x.verifyBatchNumber
    })
    this.submitModel.applyDescription = this.applyDescription
    this.submitModel.confirm = 1
    this.caseApplyRecordService.applyVerification(this.submitModel).subscribe(
      data => {
        console.log(data,"data")
        this.$message.success("提交成功")
        this.refreshData()
        this.close()
      }
    )
  }
  //  private mounted(){
  //   //  this.fileList = this.applyRecordfileList?this.applyRecordfileList:[]
  //   //  this.applyDescription = this.applyRecordDescription?this.applyRecordDescription:[]
  //  }
  private created(){
    // this.dataBoxSelectionList = this.selectionList
    this.dataBoxSelectionLists = this.selectionList
  }
  //统计坏账金额钱数
  private getBadAmount(data){
    this.caseApplyRecordService.getBadAmount(data).subscribe(
      data => {
        this.totalBadAmount = data
      }
    )
  }
  //统计案件金额总数
  private getTotalOverdueAmount(data){
    this.caseApplyRecordService.getTotalOverdueAmount(data).subscribe(
      data => {
        this.totalOverdueAmount = data
      }
    )
  }
  private refresh() {
    const fileUpload: any = this.$refs['file-upload']
    fileUpload.reset()
    this.applyDescription = ''
    this.dataSet = []
    this.searchModel = {
      caseNumber: "",
      personalName: "",
      personalNo:'',
      verifyBatchNumber: "",
    }
    this.dataBoxSelectionLists = []
    this.dataBoxSelectionList = []
  }
}
</script>

<style scoped>
.group {
  margin-bottom: 0px !important;
  margin-top: 5px;
}
</style>
